package net.wanho.shop.controller;


import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import net.wanho.shop.dto.AddressDTO;
import net.wanho.shop.dto.OrderDTO;
import net.wanho.shop.po.Order;
import net.wanho.shop.service.OrderService;
import net.wanho.shop.vo.AjaxResult;
import net.wanho.shop.vo.OrderDetailVO;
import net.wanho.shop.vo.PageVo;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;


/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author 猛虎王
 * @since 2024-12-24
 */
@Api(tags = "10.订单表接口")
@RestController
@RequestMapping("/orders")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    @ApiOperation("订单表分页查询")
    @GetMapping
    public AjaxResult findPage(@ApiParam("页码") @RequestParam(defaultValue = "1") int pageNum,
                               @ApiParam("页大小") @RequestParam(defaultValue = "5") int pageSize,
                               @ApiParam("查询条件") OrderDTO orderDTO) {
        PageVo pageVo = orderService.findAll(pageNum, pageSize, orderDTO);
        return AjaxResult.success(pageVo);
    }

    @ApiOperation("根据主键查询订单表")
    @GetMapping("/{orderId}")
    public AjaxResult findById(@PathVariable Integer orderId) {
        List<OrderDetailVO> orderDetailVO = orderService.findById(orderId);
        return AjaxResult.success(orderDetailVO);
    }

    @ApiOperation("新增订单表")
    @PostMapping
    public AjaxResult add(@RequestBody Order order) {
        orderService.add(order);
        return AjaxResult.success();
    }

    @ApiOperation("修改订单表")
    @PutMapping("{orderId}/{status}")
    public AjaxResult modify(@PathVariable Integer orderId, @PathVariable Integer status) {
        orderService.modify(orderId, status);
        return AjaxResult.success();
    }

    @ApiOperation("根据主键删除订单表")
    @DeleteMapping("/{orderId}")
    public AjaxResult removeById(@PathVariable Serializable orderId) {
        orderService.removeById(orderId);
        return AjaxResult.success();
    }

    @ApiOperation("订单发货")
    @PutMapping("send/{orderId}/{no}")
    public AjaxResult send(@PathVariable Integer orderId, @PathVariable String no) {
        orderService.send(orderId, no);
        return AjaxResult.success();
    }

    @ApiOperation("修改地址")
    @PutMapping("address/{orderId}")
    public AjaxResult changeAddress(@PathVariable Integer orderId, @RequestBody AddressDTO address) {
        orderService.changeAddress(orderId, address);
        return AjaxResult.success();
    }

    @ApiOperation("查看物流信息")
    @GetMapping("logistics/{no}")
    public AjaxResult findLogistics(@PathVariable String no) {
        JSONArray logistics = orderService.findLogistics(no);
        return AjaxResult.success(logistics);
    }

    @ApiOperation("提醒发货")
    @GetMapping("reminder/{orderId}")
    public AjaxResult Reminder(@PathVariable Integer orderId) {
        orderService.reminder(orderId);
        return AjaxResult.success();
    }
}
